package leetcode.bit.common;

import java.util.ArrayList;
import java.util.List;

public class BitUtils {

    public static int hammingWeight(int n) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            result += (n >> i) & 1;
        }
        return result;
    }

    public static int hammingWeight2(int n) {
        int result = 0;
        while (n != 0) {
            // 将 n 最后一位 1 变为 0
            n = n & (n - 1);
            result++;
        }
        return result;
    }

    public static int singleNumber(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        for (int i = 1; i < nums.length; i++) {
            nums[0] ^= nums[i];
        }
        return nums[0];
    }

    public static int singleNumberPro(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        int[] bits = new int[32];
        for (int num : nums) {
            for (int i = 0; i < 32; i++) {
                bits[i] += (num >> i) & 1;
            }
        }
        int result = 0;
        for (int i = bits.length - 1; i >= 0; i--) {
            result = (result * 2) + (bits[i] % 3);
        }
        return result;
    }

    public static int reverseBits(int n) {
        int result = 0;
        for (int i = 0; i < 32; i++) {
            // >> 是算术右移, 最高位补符号位, >>> 是逻辑右移, 最高位补0, 两者不同
            // 此处均可
            result = (result << 1) | ((n >> i) & 1);
//            result = (result << 1) | ((n >>> i) & 1);
        }
        return result;
    }

    public static boolean isPowerOfTwo(int n) {
        if (n <= 0) {
            return false;
        }
        int m = 1;
        while (m <= n) {
            if (m == n) {
                return true;
            } else {
                m = m << 1;
            }
            if (m < 0) {
                break;
            }
        }
        return false;
    }

    public static int[] countBits(int n) {
        int[] result = new int[n+1];
        for(int i=0; i<= n; i++){
            int count = 0;
            for(int j = 0; j < 32; j++){
                if(((i >> j) & 1) == 1){
                    count ++;
                }
            }
            result[i] = count;
        }
        return  result;
    }

    public static boolean makeStringsEqual(String s, String target) {
        return s.contains("1") && target.contains("1") || !s.contains("1") && !target.contains("1");
    }

    public static List<Integer> circularPermutation(int n, int start) {
        List<Integer> ret = new ArrayList<Integer>();
        ret.add(start);
        for (int i = 1; i <= n; i++) {
            int m = ret.size();
            for (int j = m - 1; j >= 0; j--) {
                ret.add(((ret.get(j) ^ start) | (1 << (i - 1))) ^ start);
            }
        }
        return ret;
    }
}
